﻿using System.Threading;

namespace eLoop;

sealed class ThreadSyncContext : ASyncContext
{
    public ThreadSyncContext(ASingleScheduler scheduler)
    {
        this.scheduler = scheduler;
    }
    
    private ASingleScheduler scheduler;
    private long disposed;

    public override void Post(SendOrPostCallback d, object state)
    {
        if (Interlocked.Read(ref this.disposed) == 1)
        {
            return;
        }
        
        this.scheduler.Schedule2(d, state);
    }

    public override void Dispose()
    {
        if (Interlocked.CompareExchange(ref this.disposed, 1, 0) == 0)
        {
            //处理
        }
    }
}